<?php
namespace App\Repositories\Admin;

use App\Models\Admin\OrderTempModel;
use App\Repositories\BaseRepository;

class OrderTempRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new OrderTempModel();
    }

    /**
     * 操作分页获取
     */
    public function getIndexList()
    {
        $models = $this->repository->orderBy('id','asc')
            ->paginate(10);
        $models->limit = 10;
        return $models;
    }

    /**
     * 通过货单ID，获取工厂列表
     */
    public function getListByBale($bale)
    {
        return $this->repository->where('bale',$bale)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 获取对应企业的订单
     * type：1公司，2工厂
     */
    public function getIndexTemp($type,$uid)
    {
        if ($type==1) {
            $query = $this->repository->where('cid',$uid);
        } else {
            $query = $this->repository->where('fid',$uid);
        }
        return $query->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    /**
     * 通过公司ID、工厂ID，获取
     */
    public function setPartner($cid,$fid)
    {
        return $this->repository->where('cid',$cid)
            ->where('fid',$fid)
            ->update(array('status'=>4));
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    /**
     * 通过公司ID、工厂ID，获取
     */
    public function getShowOneByUid($cid,$fid)
    {
        return $this->repository->where('cid',$cid)
            ->where('fid',$fid)
//            ->where('status',4)
            ->first();
    }

    /**
     * 通过货物ID，获取
     */
    public function getShowOneByBale($bale)
    {
        return $this->repository->where('bale',$bale)->first();
    }

    /**
     * 通过货物ID，获取成功订单
     */
    public function getShowOneByBale2($bale)
    {
        return $this->repository->where('bale',$bale)
            ->where('status',4)
            ->first();
    }

    /**
     * 通过公司ID，获取
     */
    public function getShowOneByComId($cid)
    {
        return $this->repository->where('cid',$cid)->first();
    }

    /**
     * 通过工厂ID，获取
     */
    public function getShowOneByFacId($cid)
    {
        return $this->repository->where('fid',$cid)->first();
    }

    /**
     * 抢单状态
     */
    public function geStatusList()
    {
        return $this->repository->getStatuss();
    }

    /**
     * 通过公司设置状态
     */
    public function setStatusCom($com,$data)
    {
        return $this->repository->where('company',$com)->update($data);
    }

    /**
     * 通过工厂设置状态
     */
    public function setStatusFac($factory,$data)
    {
        return $this->repository->where('factory',$factory)->update($data);
    }

    /**
     * 抢单类型名称
     */
    public function getStatusName($status)
    {
        $statuss = $this->geStatusList();
        return array_key_exists($status,$statuss) ? $statuss[$status] : '';
    }
}